import { defineStore } from 'pinia'
const orderSotre = defineStore("order", {
	state: () => {
		return {
			orderDetail: {//订单详情缓存

			},
			shopList: [] as any,//购物车商品列表
		}
	},
	persist: {
		enabled: true,
		H5Storage: window?.localStorage,
		strategies: [
			{
				key: 'order',
				storage: window?.sessionStorage
			}
		]
	},
	actions: {
		setOrderDetail(obj) {
			this.orderDetail = obj
		},
		minus(index) {
			this.shopList.splice(index, 1)
		},
		setShop(obj : any) {//添加-删除购物车
			console.log(obj, "obj");
			console.log(obj.goodsId, '234');
			if (!obj) {
				return uni.showToast({
					title: '添加失败',
					icon: "error"
				})
			}
			if (this.shopList.length > 50) {
				return uni.showToast({
					title: "购物车最多可添加50条",
					icon: 'none'
				})
			}

			// 添加购物车商品逻辑 -> 先判断商品类型
			// goodstype 1 普通 2 套餐 variantState 1 自动设置变体 默认选择 2 主动设置变体 就是可选多规格
			//先判断是什么类型的商品  意大利面 goodsType: 1 variantState: 1  汉堡goodsType: 2 variantState: 1 鸡尾酒 goodsType: 1 variantState: 2  蛋糕 goodsType: 1 variantState: 2
			if (obj.goodsType == 1 && obj.variantState == 1) {
				//单品
				if (this.shopList.some((item : any) => item.goodsId == obj.goodsId)) {
					this.shopList.find((item : any, index) => {
						if (item.goodsId == obj?.goodsId) {
							if (obj.nums > 0) {
								this.shopList.splice(index, 1, obj)
							}
							else {
								this.shopList.splice(index, 1)
							}
						}
					})
				} else {
					if (Object.keys(obj).length > 0) {
						this.shopList.push(obj)
					}
				}
			}
			if (obj.goodsType == 2 && obj.variantState == 1) {
				// 检查套餐是否存在
				const existingPackageIndex = this.shopList.findIndex(itemA =>
					itemA.goodsId === obj?.goodsId && itemA.relationVariantIds.join(',') === obj?.relationVariantIds.join(',')
				);

				if (existingPackageIndex !== -1) {
					// 只增加当前套餐商品的 nums
					if (obj.nums == 1) {
						this.shopList[existingPackageIndex].nums++;
					} else {
						this.shopList[existingPackageIndex].nums += obj.nums
					}

				} else {
					if (Object.keys(obj).length > 0) {
						this.shopList.push(obj);
					}
				}
			}
			if (obj.goodsType == 1 && obj.variantState == 2) {
				//多规格
				const existingPackageIndex = this.shopList.findIndex(itemA =>
					itemA.goodsId === obj?.goodsId && itemA.sku === obj?.sku)
				if (existingPackageIndex !== -1) {
					// 只增加当前套餐商品的 nums
					if (obj.nums == 1) {
						this.shopList[existingPackageIndex].nums++;
					} else {
						this.shopList[existingPackageIndex].nums += obj.nums
					}

				} else {
					if (Object.keys(obj).length > 0) {
						this.shopList.push(obj);
					}
				}
			}
		},
		shopDelAll() {//清空购物车
			uni.showModal({
				title: '提示',
				content: '是否清空购物车',
				success: (res) => {
					if (res.confirm) {
						this.shopList = []
					}
				}
			})
		},
		toSettle() {//去结算
			if (this.shopList.length == 0) {
				return uni.showToast({
					title: '购物车为空',
					icon: 'none'
				})
			}
			
			uni.navigateTo({
				url: '/subpackage/order/goods/payment?list=' + encodeURIComponent(JSON.stringify(this.shopList))
			})

		}


	},
	getters: {
		salePriceToal(state) {
			return state.shopList.reduce((res, item : any) => {
				return res += item.salePrice * item.nums
			}, 0)
		},
		price(state) {
			return state.shopList.reduce((res, item : any) => {
				return res += item.price * item.nums
			}, 0)
		},
		callshopList(state) {
			return state.shopList.filter((item : any) => item.nums)
		}
	}
})
export default orderSotre